//
//  ClassesPickerView.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/11/8.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "ClassesPickerView.h"
#import "YunOrderClassesModel.h"
#define kSelfHeight (370*ADAPTER_WIDTH)
#import "WPPPickerTopView.h"
@interface ClassesPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)UIControl *bgControl;
@property(nonatomic,strong)WPPPickerTopView *topView;
@property(nonatomic,strong)UIPickerView *pickerView;
@end

@implementation ClassesPickerView
- (void)makeUI {
    [super makeUI];
    self.backgroundColor = [UIColor whiteColor];
    [self.bgControl addSubview:self];
    self.frame = CGRectMake(0, HEIGHT, WIDTH, kSelfHeight);
    [self topView];
}
- (void)handleEvent {
    [super handleEvent];
    
}
- (void)bindViewModel {
    [super bindViewModel];
    [self startAnimation];
}
- (void)setParentVC:(BaseVC *)parentVC
{
    _parentVC = parentVC;
    [parentVC.view addSubview:self.bgControl];
}
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.topView.title = title;
}
- (void)setData:(NSArray<YunOrderClassesModel *> *)data {
    _data = data;
    [self.pickerView reloadAllComponents];
}
#pragma mark - 点击事件
- (void)clickBgControl:sender
{
    [self endAnimation];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.21 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeAllSubviews];
        [self.bgControl removeAllSubviews];
        [self.bgControl removeFromSuperview];
    });
}
- (void)clickSureButton
{
    !self.selected ?: self.selected(self.data[[self.pickerView selectedRowInComponent:0]]);
    [self clickBgControl:nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.data.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44*ADAPTER_WIDTH;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (view == nil) {
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width - 100*ADAPTER_WIDTH, 44)];
        lab.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = [UIColor blackColor];
        lab.textAlignment = NSTextAlignmentCenter;
        view = lab;
    }
    UILabel *lab = (UILabel *)view;
    lab.text = [NSString stringWithFormat:@"%@",self.data[row].parentName];
    return lab;
}
#pragma mark - 动画
- (void)startAnimation
{
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, HEIGHT - kSelfHeight, WIDTH, kSelfHeight);
    }];
}
- (void)endAnimation
{
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, HEIGHT, WIDTH, kSelfHeight);
    }];
}
#pragma mark - Lazy懒加载区域
- (UIControl *)bgControl
{
    if (!_bgControl)
    {
        UIControl *bgControl = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        [[UIApplication sharedApplication].keyWindow addSubview:bgControl];
        bgControl.backgroundColor = rgba(0, 0, 0, 0.4);
        [bgControl addTarget:self action:@selector(clickBgControl:) forControlEvents:UIControlEventTouchDown];
        _bgControl = bgControl;
    }
    return _bgControl;
}
- (WPPPickerTopView *)topView
{
    if (!_topView) {
        WPPPickerTopView *view = [[WPPPickerTopView alloc]initWithFrame:CGRectMake(0, 0, self.width, 50*ADAPTER_WIDTH)];
        [self addSubview:view];
        WK(weakSelf)
        [view setCallBackTypeBlock:^(BOOL cancel) {
            if (cancel) {
                [weakSelf clickBgControl:nil];
            } else {
                [weakSelf clickSureButton];
            }
        }];
        _topView = view;
    }
    return _topView;
}
- (UIPickerView *)pickerView
{
    if (!_pickerView) {
        UIPickerView *view = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.topView.bottom, WIDTH, self.height - self.topView.height)];
        view.delegate = self;
        view.dataSource = self;
        view.showsSelectionIndicator = YES;
        view.backgroundColor = [UIColor whiteColor];
        [self addSubview:view];
        _pickerView = view;
    }
    return _pickerView;
}
@end
